package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyardserver.WeightChangeHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;




public class CMDPrintHandle implements ICMDHandle {
    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    Handler handler = null;

    public CMDPrintHandle() {
        handler = new Handler(Looper.getMainLooper());
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }

    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {

        if(this.mSteelyardDataModule==null)
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);

        if(this.mSteelyardServerModule==null)
        this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        WeightChangeInfo weightChangeInfo= WeightChangeHelp.createByData(steelyardInfo, cmdEntity.getNum(),cmdEntity.getData());


        if(weightChangeInfo==null){
            P.p("======收到的weightChangeInfo为null");
            return null;
        }
        if(P.isDebug()) {
            String logs=cmdEntity.getSteelyardMac() + new Gson().toJson(weightChangeInfo);
            P.p("======CMD称重打印：" +logs );
            if(UIP.isPrint()){
                UIP.p("CMD打印："+logs,cmdEntity.getSteelyardMac());
            }
        }
        weightChangeInfo.isUseThisDate = false;



        
        
        


        
        if(cmdEntity.is32Zero()){
            
            if(steelyardInfo!=null&&steelyardInfo.hasState(SteelyardInfo.RunTimeState_salesReturn)){
                
                return null;
            }
        }

        if(steelyardInfo == null){
            return null;
        }

        boolean isSalesJoin = false;
        if(cmdEntity.getCmd() == (byte)0x13){
            isSalesJoin = true;
            steelyardInfo.addState(SteelyardInfo.RunTimeState_salesJoin);
        }else {
            steelyardInfo.addState(SteelyardInfo.RunTimeState_isPrint);
        }

        

        if(weightChangeInfo.isSalesReturn()){
            
            steelyardInfo.addState(SteelyardInfo.RunTimeState_salesReturn);

            
            if(weightChangeInfo.getWeight()==0){
                
                weightChangeInfo.setWeight(1);
                weightChangeInfo.setUnitPrice(weightChangeInfo.getTotalPrice());

            }

        }else {
            
            steelyardInfo.removeState(SteelyardInfo.RunTimeState_salesReturn);
        }

        

        
        if(!steelyardInfo.hasState(SteelyardInfo.RunTimeState_subtotal)) {
            
            mSteelyardDataModule.weightChange(cmdEntity.getSteelyardMac(), weightChangeInfo);//,goodId,goodName,weightChangeInfo.getUnitPrice(),weightChangeInfo.getWeight());

        }else{
            if(ExpandConfig.instance.isUI()){

                // 移除着2个状态 不然下次 称重Ad上来会触发com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle.CMDPrintHandle.xx
                steelyardInfo.removeState(SteelyardInfo.RunTimeState_isPrint);
                steelyardInfo.removeState(SteelyardInfo.RunTimeState_salesJoin);

                OnSteelyardKeyEvent.sendKey(OnSteelyardKeyEvent.KEYCODE_PAY, handler);

            }else {
                
                xx(mSteelyardDataModule, mSteelyardServerModule, cmdEntity.getSteelyardMac(), steelyardInfo, isSalesJoin);
            }
        }


//        OrderEntity orderEntity = mSteelyardDataModule.print(cmdEntity.getSteelyardMac(), isSalesJoin);



////        mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());



//            mSteelyardServerModule.print(cmdEntity.getSteelyardMac(), "打单失败。", "");


//            String print= PrintHelp.print(orderEntity,SteelardHelp.formatMoney(orderEntity.totalPrice),orderEntity.goods,orderEntity.createTime,steelyardInfo,isSalesJoin);

//            String qrCode= com.lkm.market2_0.steelard_server.Config.createQrCode(steelyardInfo,orderEntity.orderNum);

//                mSteelyardServerModule.print(cmdEntity.getSteelyardMac(), print,qrCode);




        

        
        steelyardInfo.removeState(SteelyardInfo.RunTimeState_subtotal);

        return null;
    }


    public static void xx(SteelyardDataModule mSteelyardDataModule, SteelyardServerModule mSteelyardServerModule, String steelyardMac, SteelyardInfo steelyardInfo, boolean isSalesJoin) {
        OrderEntity orderEntity = mSteelyardDataModule.print(steelyardMac, isSalesJoin);
        
        if (orderEntity == null || steelyardInfo == null) {
            mSteelyardServerModule.print(steelyardMac, "打单失败。", "");
        } else {
            String print = PrintHelp.print(orderEntity, SteelardHelp.formatMoney(orderEntity.totalPrice), orderEntity.getGoods(), orderEntity.createTime, steelyardInfo, isSalesJoin);
            String qrCode = com.lkm.market2_0.m_imple.steelyardserver.server.Config.createQrCode(steelyardInfo, orderEntity.orderNum);
            mSteelyardServerModule.print(steelyardMac, print, qrCode);
        }

        
        steelyardInfo.removeState(SteelyardInfo.RunTimeState_isPrint);
        steelyardInfo.removeState(SteelyardInfo.RunTimeState_salesJoin);
    }




}
